import $ from 'jQuery';
(function () {
    /**
     * 删除账户
     * @param params
     * @private
     */
    var _deleteProject = function (params) {
        IOT.confirm('你确定要删除吗？', () => {
            IOT.showOverlay('保存中，请稍等...');
            IOT.getServerData(URI.OPERATE.PROJECT.DELETE, params, function (ret) {
                IOT.hideOverlay();
                if (ret && ret.code === 200) {
                    IOT.tips('删除成功！', 'success');
                    M.Table.refresh.all();
                    BootstrapDialog.closeAll();
                } else {
                    IOT.tips(ret.msg || '操作失败，请重试！', 'error');
                }
            });
            return false;
        }, null, 'warn');
    };
    
    let $vue = new Vue({
        el: '.main-hook',
        data: {
            leaseType: [{id: 1, name: '短租'}, {id: 2, name: '长租'}],
            receiveStatus:[{id: 1, name: '部分收货'}, {id: 2, name: '全部收货'},{id: 3, name: '订单取消'}],
            orderStatus  : [{id: 0, name: '未审核'}, {id: 1, name: '已审核'}, {id: 2, name: '已完结'}, {id: 3, name: '已废弃'}],
            paymentFlag:[{id:1,name:'未打款'},{id:2,name:'已打款至第三方银行'},{id:3,name:'已收款'}],
            boxs: {
                box1: {show: true, hook: '.box1-hook', lock: true, url: '', params: {}},
                box2: {show: false, hook: '.box2-hook', url: '', params: {}},
                box3: {show: false, hook: '.box3-hook', url: '', params: {}},
                box4: {show: false, hook: '.box4-hook', url: '', params: {}}
            },
            roleList: []
        },
        created: function () {
            M.Request.getRoleList(true, (roleList, ret) => {
                this.roleList = roleList;
            });
        },
        methods: {
            emitPage: function () {
                M.Page.load(this.boxs);
            },
            // 上一页
            prePage: function () {
                M.Page.prePage(this.boxs);
            },
            searchKey:function(){
                $('.search-hook').trigger('click');
            },
            // 触发刷新页(当前显示的页面)
            refreshPage: function () {
                M.Page.refreshPage(this.boxs);
            },
            // 新建项目
            createProject: function () {
                M.Page.emit(URI.OPERATE.PROJECT.CREATE.PAGE);
            }
        },
        mounted: function () {
            let thiz = this;
            this.$nextTick(() => {
                let tableHookName = '.project-table-hook';
                let $accountBox = $('.project-hook');
                // 省份-城市-区域
                //初始化，默认不选择任何一个
                var distpicker = {
                    init:function () {
                        $('#distpicker').distpicker();
                    }
                };
                //初始化-省份、城市、区县
                distpicker.init();
                $('.resert').on('click', function () {
                    $("#distpicker").distpicker('destroy');
                    $("#distpicker").distpicker({
                        autoselect: 0,
                        province: '— 省 —',
                        city: '— 市 —',
                    });
                });
                //初始化时间插件
                modal.initDate("startdate","enddate");
                var $form = $accountBox.find('.form-hook');
                // 查询
                $form.off('submit').on('submit', function () {
                    $(tableHookName).bootstrapTable('onCustomSearch');
                    return false;
                });
                var $tableHook = $accountBox.find(tableHookName).bootstrapTable({
                    scrollbar: 'project-table-body',
                    striped: true, // 设置为 true 会有隔行变色效果
                    pagination: true, // true 显示分页
                    paginationDetail: true, // 分页详情
                    sidePagination: 'server', // 服务器端
                    method: 'post',
                    url: URI.OPERATE.PROJECT.LIST,
                    debug: false,
                    cache: false,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    // fixedColumns: true,
                    customButton: '',
                    customQueryParams: function (params) {
                        var formParams = $form.serializeJson();
                        var queryParams = $.extend({}, params, formParams);
                        //console.log(queryParams);
                        return queryParams;
                    },
                    onLoadSuccess: function (data) {
                        //console.log(JSON.stringify(data));
                    },
                    onLoadError: function (status, xhr) {
                    },
                    onCheckAll: function (rows) {
                        
                    },
                    columns: [
                        /*{
                         field: '', checkbox: true, align: 'center', fixedLeft: true, width: 60,
                         events: {
                         'change .checkbox-hook': function (e, value, row, index) {
                         console.log('复选框: ' + value);
                         }
                         }
                         },*/
                        /*{
                         field: 'no', title: '序号', align: 'center', width: 50,tips:true,
                         formatter: function (value, row, index) {
                         var pageSize =  $accountBox.find(tableHookName).bootstrapTable('getOptions').pageSize;     //通过table的#id 得到每页多少条
                         var pageNumber = $accountBox.find(tableHookName).bootstrapTable('getOptions').pageNumber; //通过table的#id 得到当前第几页
                         return pageSize * (pageNumber - 1) + index + 1;    // 返回每条的序号： 每页条数 *（当前页 - 1 ）+ 序号
                         }
                         },*/
                        {
                            field: 'mobile', title: '项目编号', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'userName', title: '项目名称', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'gender', title: '分组数', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'email', title: '单灯数', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'email', title: '所属省', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'email', title: '所属市', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'registerTime', title: '创建时间', align: 'center', fixedLeft: true,tips:true,
                            formatter: function (val, row, index) {
                                return val;
                            }
                        },
                        {
                            field: 'operate', title: '操作', align: 'center', fixedLeft: true,width: 180,
                            events: {
                                'click .detail-project': function (e, value, rowData, index) {
                                    M.Page.emit(URI.OPERATE.PROJECT.DETAIL.PAGE);
                                    IOT.setSessionStore(URI.OPERATE.PROJECT.DETAIL.PAGE, JSON.stringify(rowData));
                                    return false;
                                },
                                'click .update-project': function (e, value, rowData, index) {
                                    M.Page.emit(URI.OPERATE.PROJECT.UPDATE.PAGE);
                                    IOT.setSessionStore(URI.OPERATE.PROJECT.UPDATE.PAGE, JSON.stringify(rowData));
                                    return false;
                                },
                                'click .delete-project': function (e, value, row, index) {
                                    _deleteProject(row);
                                    return false;
                                }
                            },
                            formatter: function (value, row, index) {
                                let html = [];
                                if (M.Authority.checkAuthority('account-detail')) {
                                    html.push('<a class="detail-project operate">查看</a>');
                                }
                                if (M.Authority.checkAuthority('account-modify')) {
                                    html.push('<a class="update-project operate">修改</a>');
                                }
                                if (M.Authority.checkAuthority('account-delete')) {
                                    /*if (M.Authority.checkAuthority('account-delete')) {*/
                                        html.push('<a class="delete-project operate">删除</a>');
                                    /*}*/
                                }
                                return html.join('');
                            }
                        }
                    ]
                });
                //权限——查看
                if (!M.Authority.checkAuthority('account-detail')&&!M.Authority.checkAuthority('account-modify')) {
                    $(tableHookName).bootstrapTable('hideColumn', 'operate');//隐藏列
                }
            });
        }
    });
})();